package com.supply.service.impl;

import com.supply.mapper.CompanyGoodsMapper;
import com.supply.mapper.CompanysMapper;
import com.supply.mapper.GoodsMapper;
import com.supply.pojo.*;
import com.supply.req.CompanyGroup;
import com.supply.req.CompanysReq;
import com.supply.service.CompanysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class CompanysServiceImpl implements CompanysService {
    @Autowired
    private CompanysMapper companysMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CompanyGoodsMapper companyGoodsMapper;

    @Override
    public List<Companys> queryByCondition(CompanysReq companysReq) {
        CompanysExample example = new CompanysExample();
        CompanysExample.Criteria criteria = example.createCriteria();
        if (companysReq != null) {
            if (!ObjectUtils.isEmpty(companysReq.getName())) {
                criteria.andNameLike("%" + companysReq.getName() + "%");
            }
            if (!ObjectUtils.isEmpty(companysReq.getRemark())) {
                criteria.andRemarkLike("%" + companysReq.getRemark() + "%");
            }
        }
        return companysMapper.selectByExample(example);
    }

    @Override
    public int saveOrUpdate(Companys companys) {
        if (companys.getId() == null) {
            return companysMapper.insert(companys);
        } else {
            return companysMapper.updateByPrimaryKey(companys);
        }
    }

    @Override
    public int deleteById(Long id) {
        return 0;
    }

    @Override
    public int updateCooperateGoods(CompanyGroup companyGroup) {
        //处理请求的合作商品
        //遍历检查商品表是否存在请求的合作商品，不存在则添加
        for (Goods goods : companyGroup.getCooperateGoodsList()) {
            //清理请求数据的非法或不完整的数据
            if (goods.getName() == null || goods.getPrice() == null) {
                continue;
            }
            //若请求的合作商品原本存在，则执行更新操作
            if (goods.getId() != null){
                goodsMapper.updateByPrimaryKey(goods);
                continue;
            }
            //根据商品名称和单价查询合作商品，
            GoodsExample goodsExample = new GoodsExample();
            goodsExample.createCriteria()
                    .andNameEqualTo(goods.getName())
                    .andPriceEqualTo(goods.getPrice());
            List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
            //若不存在则添加，并回填id，
            if (goodsList.isEmpty()) {
                goods.setQuantity(0L);
                goodsMapper.insert(goods);
            } else {
                //若存在则直接赋予id
                goods.setId(goodsList.get(0).getId());
            }
        }

        //先根据companyId删除关联表数据
        CompanyGoodsExample companyGoodsExample = new CompanyGoodsExample();
        companyGoodsExample.createCriteria().andCompanyIdEqualTo(companyGroup.getId());
        companyGoodsMapper.deleteByExample(companyGoodsExample);

        //重新插入合作公司与合作商品的关联表数据
        for (Goods goods : companyGroup.getCooperateGoodsList()) {
            if (goods.getName() == null || goods.getPrice() == null) {
                continue;
            }
            CompanyGoods companyGoods = new CompanyGoods();
            companyGoods.setCompanyId(companyGroup.getId());
            companyGoods.setGoodsId(goods.getId());
            companyGoodsMapper.insert(companyGoods);
        }

        return 0;
    }
}
